package com.spring.beans.factory.support;

import com.spring.BeanDefinition;
import com.spring.beans.BeansException;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;

/**
 * @Author: Asuka
 * @Description:
 * @create: 2025-04-14 15:49
 */


public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy{

    //使用CGLIB动态生成子类
    @Override
    public Object instantiate(BeanDefinition beanDefinition) throws BeansException {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(beanDefinition.getBeanClass());
        enhancer.setCallback((MethodInterceptor)(obj, method, argsTemp, proxy)->proxy.invokeSuper(obj,argsTemp));
        return enhancer.create();
    }
}
